// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "company": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "maxLength": 100
      }
    ],
    "title": "所属公司",
    "label": "所属公司"
  },
  "contact": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "maxLength": 50
      }
    ],
    "title": "联系人",
    "label": "联系人"
  },
  "phone": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "maxLength": 20
      }
    ],
    "title": "联系方式",
    "label": "联系方式"
  },
  "exchange": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "range": [
          {
            "value": "上期/能源所",
            "text": "上期/能源所"
          },
          {
            "value": "中金所",
            "text": "中金所"
          },
          {
            "value": "大商所",
            "text": "大商所"
          },
          {
            "value": "郑商所",
            "text": "郑商所"
          },
          {
            "value": "广期所",
            "text": "广期所"
          }
        ]
      }
    ],
    "title": "待测试交易所",
    "label": "待测试交易所"
  },
  "accountCount": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "int"
      },
      {
        "minimum": 1,
        "maximum": 100
      }
    ],
    "title": "测试账号数量",
    "label": "测试账号数量"
  },
  "futuresCompany": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "maxLength": 100
      }
    ],
    "title": "所属期货公司",
    "label": "所属期货公司"
  },
  "futuresContact": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "maxLength": 50
      }
    ],
    "title": "期货公司对接人",
    "label": "期货公司对接人"
  },
  "status": {
    "rules": [
      {
        "format": "string"
      },
      {
        "range": [
          {
            "value": "pending",
            "text": "pending"
          },
          {
            "value": "approved",
            "text": "approved"
          },
          {
            "value": "rejected",
            "text": "rejected"
          }
        ]
      }
    ],
    "title": "申请状态",
    "defaultValue": "pending",
    "label": "申请状态"
  },
  "createTime": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "创建时间",
    "label": "创建时间"
  },
  "updateTime": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "更新时间",
    "label": "更新时间"
  },
  "ip": {
    "rules": [
      {
        "format": "string"
      },
      {
        "maxLength": 50
      }
    ],
    "title": "提交IP地址",
    "label": "提交IP地址"
  },
  "userAgent": {
    "rules": [
      {
        "format": "string"
      },
      {
        "maxLength": 500
      }
    ],
    "title": "用户代理",
    "label": "用户代理"
  }
}

const enumConverter = {
  "exchange_valuetotext": {
    "上期/能源所": "上期/能源所",
    "中金所": "中金所",
    "大商所": "大商所",
    "郑商所": "郑商所",
    "广期所": "广期所"
  },
  "status_valuetotext": {
    "pending": "pending",
    "approved": "approved",
    "rejected": "rejected"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
